import 'package:flutter/material.dart';
import 'package:qunaimedical/components/colored_tabbar.dart';
import 'package:qunaimedical/models/category.dart';
import 'package:qunaimedical/models/list_response.dart';
import 'package:qunaimedical/network/http.dart';
import 'package:qunaimedical/pages/login.dart';
import 'package:qunaimedical/pages/moment/moment_create.dart';
import 'package:qunaimedical/pages/moment/moment_list.dart';
import 'package:qunaimedical/utils/event_bus_manager.dart';
import 'package:qunaimedical/utils/sp_utils.dart';
import 'package:qunaimedical/values/urls.dart';
import 'package:sprintf/sprintf.dart';

class MomentHome extends StatefulWidget {
  const MomentHome({Key key}) : super(key: key);

  @override
  _MomentHomeState createState() => _MomentHomeState();
}

class _MomentHomeState extends State<MomentHome> {
  List<Category> _categories = [];

  @override
  void initState() {
    super.initState();
    _fetchCategories();
  }

  @override
  Widget build(BuildContext context) {
    return DefaultTabController(
      length: _categories.length,
      child: Scaffold(
        appBar: AppBar(
          title: Text('群爱圈'),
          bottom: ColoredTabBar(
            color: Colors.white,
            width: MediaQuery.of(context).size.width,
            tabBar: TabBar(
              isScrollable: true,
              labelColor: Theme.of(context).primaryColor,
              unselectedLabelColor: Colors.black54,
              labelPadding:
                  EdgeInsets.symmetric(vertical: 12.5, horizontal: 10),
              indicatorSize: TabBarIndicatorSize.label,
              indicator: UnderlineTabIndicator(
                  borderSide: BorderSide(
                      width: 2, color: Theme.of(context).primaryColor)),
              tabs: _categories.map((e) => Text(e.label)).toList(),
            ),
          ),
        ),
        body: TabBarView(
            children: _categories.map((e) => MomentList(category: e)).toList()),
        floatingActionButton: FloatingActionButton(
          onPressed: () {
            if (SPUtils.getUser() == null) {
              Navigator.push(
                  context, MaterialPageRoute(builder: (c) => LoginPage()));
            } else {
              Navigator.push(
                context,
                MaterialPageRoute(
                  builder: (c) => MomentCreatePage(categories: _categories),
                ),
              ).then((value) {
                if (value != null && value) {
                  eventBus.fire(EventBusManager(EventBusManager.momentRefresh));
                }
              });
            }
          },
          heroTag: 'moment_create',
          child: Icon(Icons.add),
          backgroundColor: Theme.of(context).primaryColor,
          mini: true,
        ),
      ),
    );
  }

  void _fetchCategories() async {
    var data =
        await XHttp.postJson(sprintf(Urls.DICT_VALUES, ['post_info_type']));
    if (data != null) {
      ListResponse response = ListResponse.fromJson(data);
      if (response.code == 1) {
        if (mounted) {
          setState(() {
            _categories =
                List.from(response.data.map((e) => Category.fromJson(e)));
            _categories.sort((a, b) => a.sort.compareTo(b.sort));
          });
        }
      }
    }
  }
}
